import pathlib, sys, logging, threading
from urllib import request
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# create logger
module_logger = logging.getLogger('get_pdf.utils')

SEARCH_URL="https://find-and-update.company-information.service.gov.uk/company/{}/filing-history"

def create_chrome(driver_path):
  options = webdriver.ChromeOptions()
  options.add_argument('--headless')
  options.add_argument('--disable-gpu')
  options.add_experimental_option('excludeSwitches', ['enable-logging'])
  driver = webdriver.Chrome(executable_path=driver_path,options=options)

  return driver

def check_exists_by_xpath(driver, xpath):
  try:
    driver.find_element(By.XPATH,xpath)
  except NoSuchElementException:
    return False
  return True

def save_url_of_account(company):
  """
  Open company page of 'Filing history'
  If description contains keyword, save link into txt file
  """
  output_path = pathlib.Path('./Company/'+company)
  output_path.mkdir(parents=True, exist_ok=True)
  output_file=output_path.joinpath(company+'_account_url'+".txt")

  driver=create_chrome("../drivers/chromedriver")
  try:
    searchurl=SEARCH_URL.format(company)
    # searchurl="https://find-and-update.company-information.service.gov.uk/company/{}/filing-history".format(company)
    driver.get(searchurl)

    #checkin account checkbox
    chkAcct=driver.find_element(By.ID, 'filter-category-accounts')
    chkAcct.click()

    with open(output_file, 'w') as fo:
      while 1 :
        if check_exists_by_xpath(driver,"//div[@id='filing-history-content']") :
          module_logger.info('find id filing-history-content')
          div = driver.find_element(By.ID,'filing-history-content')
          alist = div.find_elements(By.CSS_SELECTOR,'tr td div a.download')
          it = iter(alist)
          for x in it:
            fo.write(x.get_attribute('href')+'\n')
        else:
          break
        if check_exists_by_xpath(driver,"//a[@id='nextButton']") :
          driver.find_element(By.ID,'nextButton').click()
        else:
          break

    driver.quit()
  except Exception as e:
    module_logger.error(f'Exception-save_filing_pdf-company:{company}', exc_info=True)
    driver.quit()
    raise


def download(link, filelocation):
  try:
    request.urlretrieve(link, filelocation)
  except Exception as e:
    module_logger.error("Exception in download()", exc_info=True)

def createNewDownloadThread(link, filelocation):
  download_thread = threading.Thread(target=download, args=(link,filelocation))
  download_thread.start()

def save_pdf_by_url(company):
  try:
    company_path = pathlib.Path('./Company/'+company)
    urlfile=company+'_account_url.txt'
    urlfile_path = company_path.joinpath(urlfile)
    if pathlib.Path.is_file(urlfile_path):
      print(urlfile + ' exists.', flush=True)
      output_path=company_path.joinpath('pdf')
      output_path.mkdir(parents=True, exist_ok=True)
      with open(urlfile_path) as f:
        for line in f:
          url=line.strip()
          if url == 0:
            break;
          start=url.find("filing-history/")+len("filing-history/")
          end=url.find("/document")
          fileid=url[start:end]
          local_file=output_path.joinpath(fileid+'.pdf')
          # use urllib.request
          pdf_url = request.build_opener().open(request.Request(url)).geturl()
          createNewDownloadThread(pdf_url, local_file)
    else:
      print(urlfile + ' not found.', flush=True)
  except Exception as e:
    module_logger.error(f'Exception-save_filing_pdf-company:{company},url:{url}', exc_info=True)
    raise